<?php
/* 
* @Title:  [商品管理模型]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-07 11:07:21
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-27 22:18:44
* @Copyright:  [hn7m.com]
*/
class GoodsControl extends CommonControl{
	private $model=NULL;
	private $goodsView=NULL;
	private $combine=NULL; //货品表
	/**
	 * [__init 自动加载模型类]
	 * @return [type] [description]
	 */
	public function __init(){
		parent::__init();
		$this->model=K('Goods');
		$this->goodsView=K('GoodsView');
		$this->combine=K('Combine');   //自动加载货品表
	}
	/**
	 * [index 默认显示商品列表]
	 * @return [type] [description]
	 */
	public function index(){
		$goods=$this->model->get_data($field=NULL,$where=NULL,'gid ASC');
		$this->assign('goods',$goods);
		$this->display();
	}
	/**
	 * [add_goods 添加商品]
	 */
	public function add_goods(){
		$topCate=K('Category')->get_data(array('cid','cname'),array('cpid'=>0),'csort ASC');
		$this->assign('topCate',$topCate);//分配顶级分类

		$type=K('Type')->get_data();
		$this->assign('type',$type);  //分配商品类型

		$chan=K('Channel')->get_data();
		$this->assign('chan',$chan); //分配商品栏目

		$maxSort=K('Goods')->get_max('gsort');
		$maxSort=(isset($maxSort))?$maxSort:0;
		$this->assign('maxSort',$maxSort); //分配最大排序序号
		if(IS_POST){
			if(!$this->model->create()){    //执行自动验证
				$this->error($this->model->error);
			}
			$gid=$this->model->add_data();
			$tid=Q('post.qm_type_tid',0,'intval');
			if(!$gid){		//执行商品添加动作
				$this->error($this->model->error);
			}
			go(U('go_page',array('gid'=>$gid,'tid'=>$tid)));  //商品添加成功后跳转到中转页面
		}
		$this->display();
	}
	/**
	 * [go_page 商品添加成功后跳转的页面]
	 * @return [type] [description]
	 */
	public function go_page(){
		$gid=Q('get.gid',0,'intval');
		$tid=Q('get.tid',0,'intval');
		//这里分配是为了中转页的相关跳转
		$this->assign('gid',$gid);//分配商品gid
		$this->assign('tid',$tid);//分配类型tid
		$this->display();
	}
	/**
	 * [ajax_add_subcate ajax获得二级分类]
	 * @return [type] [description]
	 */
	public function ajax_get_subcate(){
		if(IS_AJAX){
			$cid=Q('get.cid',0,'intval');
			if($subCate=K('Category')->get_data(array('cid','cname'),array('cpid'=>$cid),'csort ASC')){
				echo $this->ajax($subCate,'json');
			}			
		}
	}
	/**
	 * [ajax_get_attr ajax添加商品属性/货品列表]
	 * @return [type] [description]
	 */
	public function ajax_add_attr(){
		if(IS_AJAX){
			//执行自动验证
			if(!$this->combine->create()){
				$return=array(
					'state'		=>0,
					'message'	=>$this->combine->error,
					'timeout'	=>3
					);
				$this->ajax($return);
			}
			//如果重复添加
			if($this->combine->add_data()){
				$return=array(
					'state'		=>1,
					'message'	=>'添加成功!',
					'timeout'	=>3
					);
			}else{
				$return = array(
					'state' => 0,
					'message' => $this->combine->error,
					'timeout'=>2
				);
			}
			$this->ajax($return);
		}
	}
	/**
	 * [ajax_del_attr ajax删除商品属性/货品列表]
	 * @return [type] [description]
	 */
	public function ajax_del_attr(){
		if(IS_AJAX){
			$bid=Q('get.bid',0,'intval');
			if($this->combine->del_data($bid)){
				$return =array(
					'state'		=>1,
					'message'	=>'删除成功!',
					'timeout'	=>2
					);
				$this->ajax($return);
			}
		}
	}
	/**
	 * [ajax_edit_attr ajax修改库存量/货品列表]
	 * @return [type] [description]
	 */
	public function ajax_edit_attr(){
		if(IS_AJAX){
			$bid=Q('post.bid',0,'intval');
			$binventory=Q('post.binventory',0,'intval');
			$data=array('binventory'=>$binventory);
			//执行自动验证
			if(!$this->combine->create()){
				$return=array(
					'state'		=>0,
					'message'	=>$this->combine->error,
					'timeout'	=>3
					);
				$this->ajax($return);
			}
			//执行修改方法
			if($this->combine->save_data($bid,$data)){
				$return=array(
					'state'		=>1,
					'message'	=>'修改成功!',
					'timeout'	=>2
					);
				$this->ajax($return);
			}
		}
	}
	/**
	 * [add_attr 添加商品属性]
	 */
	public function combine(){
		$gid=Q('get.gid',0,'intval');
		$tid=Q('get.tid',0,'intval');

		$type=$this->goodsView->join(array('type'))->get_onedate_value($gid,'ttitle');
		$name=$this->goodsView->join(array('type'))->get_onedate_value($gid,'gname');
		$nums=$this->goodsView->join(array('type'))->get_onedate_value($gid,'ginventory');
		$attrName=K('TypeView')->get_data(array('qm_type_tid'=>$tid),array('tid','aname'),'aname');
		$attr=K('Attr')->get_alldata(array('qm_type_tid'=>$tid));
		$combine=K('Combine')->get_data(array('qm_goods_gid'=>$gid));
		if(!empty($combine)){
			foreach ($combine as $key => $value) {
			$combine[$key]['bcontent']=explode(',', $value['bcontent']);
			}
			foreach ($combine as $k => $v) {
				foreach ($combine[$k]['bcontent'] as $m => $n) {
					$str=K('Attr')->get_onedate_value($n,'acontent');
					$combine[$k]['bcontent'][$m]=$str;
				}
			}
		}
		$this->assign('name',$name);   //分配当前商品名称
		$this->assign('nums',$nums);   //分配当前商品的总库存
		$this->assign('type',$type);   //分配当前商品所在类型
		$this->assign('attrName',$attrName);   //分配属性名称

		$this->assign('attr',$attr);  //分配属性值
		$this->assign('combine',$combine);  //分配货品列表数据
		$this->display();
	}
	/**
	 * [del_goods 删除商品]
	 * @return [type] [description]
	 */
	public function del_goods(){
		//删掉商品详情表相关信息
		//删掉货品列表
		//删除掉该商品下的所有评论
		//删掉购物车中该商品记录
		//删掉订单列表
		//更新订单中的总价格
		$gid=Q('gid',0,'intval');
		if($this->model->del_data($gid)){
			$this->success($this->model->success,U('Combine/del_data',array('gid'=>$gid)));
		}else{
			$this->error($this->model->error);
		}
	}
	public function edit_goods(){
		$this->display();
	}
}
?>